package com.runen.wnhz.runen.ui.activity.mine;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.popwindow.PayPasswordDialog;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.JumpUtlis;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.PersonalEntity;
import com.runen.wnhz.runen.data.entity.UploadImgEntity;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerPersonalComponent;
import com.runen.wnhz.runen.di.module.PersonalModule;
import com.runen.wnhz.runen.presenter.Contart.PersonalContart;
import com.runen.wnhz.runen.presenter.iPresenter.IPersonal;
import com.runen.wnhz.runen.ui.ActivityManager;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.activity.login.LoginForPersonalActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

/**
 * 修改密码
 */

public class ChangePayPasswordActivity extends BaseActivity<IPersonal> implements PersonalContart.View, View.OnClickListener {


    @BindView(R.id.et_change_paypaswd_phone)
    EditText etChangePaypaswdPhone;
    @BindView(R.id.et_verification_input_vcode)
    EditText etVerificationInputVcode;
    @BindView(R.id.tv_verification_code)
    TextView tvVerificationCode;
    @BindView(R.id.tv_verification_login)
    TextView tvVerificationLogin;
    private UserBean reqeustUser;
    PersonalEntity personalEntity;

    String newPasd;

    @Override
    protected int getLayoutView() {
        return R.layout.change_paypassword_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                //修改密码
                mPresenter.setUpdatePayPass();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initStart() {
        initGetUserMessage();
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this, R.color.title_green);

    }

    private void initGetUserMessage() {
        reqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);
        mPresenter.getUserMessage();
        etChangePaypaswdPhone.setText(reqeustUser.getPhone());
        etChangePaypaswdPhone.setEnabled(false);
    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("设置交易密码")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setRightTextColor(ContextCompat.getColor(this, R.color.text_white))
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }


    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {

        DaggerPersonalComponent.builder().applicationComponent(appComponent)
                .personalModule(new PersonalModule(this))
                .build().inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_verification_code:
                mPresenter.getCode();
                break;
            case R.id.tv_verification_login:
                mPresenter.setUpdatePayPass();
                break;
        }

    }

    @Override
    public void showLoadding() {

    }

    @Override
    public void dimissLoding() {

    }

    @Override
    public void showErrorMessage(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public String getToken() {
        return reqeustUser.getToken();
    }

    @Override
    public void setLogin(String info) {
        ToastUtil.showToast(info);
    }

    @Override
    public void setErrorMessage(String info) {
        ToastUtil.showToast(info);
    }

    @Override
    public void getUserMessage(PersonalEntity data) {
        personalEntity = data;
    }

    @Override
    public TextView getTextSendOut() {
        return tvVerificationCode;
    }

    @Override
    public String getPhone() {
        return etChangePaypaswdPhone.getText().toString();
    }

    @Override
    public String getPassword() {
        return newPasd;
    }

    @Override
    public String getCode() {
        return etVerificationInputVcode.getText().toString();
    }

    @Override
    public String getNickName() {
        return null;
    }

    @Override
    public String getSex() {
        return null;
    }

    @Override
    public String getSchool() {
        return null;
    }

    @Override
    public String getPlace() {
        return null;
    }

    @Override
    public String getNewPassword() {
        return null;
    }

    @Override
    public String getWornPassword() {
        return null;
    }

    @Override
    public String getRePassword() {
        return null;
    }

    @Override
    public String getHedimg() {
        return null;
    }

    @Override
    public void setSuccess(String info) {
        ToastUtil.showToast(info);
        finish();
      /*  ACacheUtlis.getInstance().isLogin(this, "false");
        RongIM.getInstance().logout();
        ACacheUtlis.getInstance().IsRequstUser(this, "", "", "", "");
        JumpUtlis.getInstance().jumpActivity(this, LoginForPersonalActivity.class);
        ActivityManager.getAppManager().finishActivity(PersonalCenterActivity.class);
        ActivityManager.getAppManager().finishActivity(this);*/
    }

    @Override
    public void setSuccess(UploadImgEntity info) {

    }

    @Override
    public File getFile() {
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_verification_code, R.id.tv_verification_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_verification_code:
                mPresenter.getCode();
                break;
            case R.id.tv_verification_login:
                final PayPasswordDialog dialog=new PayPasswordDialog(ChangePayPasswordActivity.this,R.style.mydialog);
                dialog.setDialogClick(new PayPasswordDialog.DialogClick() {
                    @Override
                    public void doConfirm(String password) {
                        newPasd = password;
                        dialog.dismiss();
                        Toast.makeText(ChangePayPasswordActivity.this,password,Toast.LENGTH_LONG).show();
                        mPresenter.setUpdatePayPass();
                    }
                });
                dialog.show();

                break;
        }
    }
}
